/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.table.cellRenderer;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.table.cellRenderer
 * @date 30/03/2014 - 16:45:55
 */
public class DateRenderer extends DefaultTableCellRenderer {
	
	private static final long serialVersionUID = 8841091604686445931L;
	
	private final SimpleDateFormat formatter;
	
	/**
	 * 
	 */
	public DateRenderer() {
	
		this(Locale.getDefault().getCountry().equals("BR") ? "dd/MM/yyyy" : "yyyy-MM-dd");
	}
	
	/**
	 * @param pattern
	 */
	public DateRenderer(final String pattern) {
	
		super();
		
		formatter = new SimpleDateFormat(pattern);
		
		setHorizontalAlignment(SwingConstants.CENTER);
	}
	
	/**
	 * @param value
	 * @return formated value or empty String
	 */
	public String format(final Object value) {
	
		// 
		if(value != null) {
			
			Calendar calendar = Calendar.getInstance();
			
			// 
			if(Time.class.isAssignableFrom(value.getClass())) {
				
				calendar.setTime((Time)value);
				
			}else if(Timestamp.class.isAssignableFrom(value.getClass())) {
				
				calendar.setTime((Timestamp)value);
				
			}else if(java.sql.Date.class.isAssignableFrom(value.getClass())) {
				
				calendar.setTime((java.sql.Date)value);
				
			}else if(Calendar.class.isAssignableFrom(value.getClass())) {
				
				calendar = (Calendar)value;
				
			}else {
				
				calendar.setTime((Date)value);
			}
			
			try {
				//
				return formatter.format(calendar.getTime());
				
			}catch(final Throwable throwable) {
				// ignore
			}
		}
		
		return "";
	}
	
	/**
	 * @param value
	 * @return Date or null
	 */
	public Date parse(final String value) {
	
		try {
			//
			return value != null ? formatter.parse(value) : null;
			
		}catch(final Throwable throwable) {
			// ignore
		}
		
		return null;
	}
	
	@Override
	public void setValue(final Object value) {
	
		setText(format(value));
	}
}
